<?php
namespace app\modules\v1\controllers;
use Yii;
use yii\rest\ActiveController;
use yii\web\Response;
use app\modules\v1\models\Merchant;
use app\modules\v1\models\Merchantinfocard;
use app\modules\v1\models\MerchantWeixin;

class InfoController extends ActiveController
{
	
    public $modelClass = 'app\modules\v1\models\info';
    
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['update'], $actions['create'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionCreate()
    {
    	$res = array();
    	if(Yii::$app->request->isPost){
    		$openid  = isset(Yii::$app->request->post()['openid'])?Yii::$app->request->post()['openid']:'';
    		if(empty($openid)){
    			$res['code'] 		= '10402';
    			$res['msg']  		= '请取消关注后重新关注公众号';
    			$response			= Yii::$app->response;
    			$response->format 	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
    		$banner				= '';
    		$merchant_code		= '';
    		$merchant_name		= '';
    		$name				= '';
    		$id_card			= '';
    		$phone				= '';
    		$address_detail		= '';
    		$bank_no			= '';
    		$bill_max			= '0.00';
    		$bill_max_day		= '0.00';
    		$identity_status	= '0';
    		$bill_rate			= '0.38%<br/>开通即送5000元免费收款大礼包';	//收款费率
    		$bill_cycles		= Yii::$app->params['bill_cycles'];	//结算周期
    		$merchant_no    	= '';
    		$status 			= '0';
    		$headimg 			= '';
    		$weixininfo = MerchantWeixin::findByOpenid($openid);
    		if(!empty($weixininfo)){
    			$name				= $weixininfo->nickname;
    			$headimg			= $weixininfo->headimg;
    			$merchant 			= new Merchant();
    			$merchant1 			= $merchant::findOne($weixininfo->merchant_id);
    			if(!empty($merchant1)){
    				$merchant_code 	= !empty($merchant1->business_invite_code)?$merchant1->business_invite_code:'';
    				if(empty($merchant1->merchant_name)){
    					$merchant_name  = base64_decode($weixininfo->nickname);
    				}else{
    					$merchant_name 	= $merchant1->merchant_name;
    				}
    				$name		   	= !empty($merchant1->merchant_real_name)?$merchant1->merchant_real_name:'';
    				$id_card		= !empty($merchant1->identity_card)?$merchant1->identity_card:'';
    				$phone			= !empty($merchant1->mobile)?$merchant1->mobile:'';
    				$address_detail	= !empty($merchant1->detail_address)?$merchant1->detail_address:'';
    				$merchant_no	= !empty($merchant1->merchant_no)?$merchant1->merchant_no:'';
    				//认证信息  0未认证；1认证正在审核；2认证未通过；3认证通过
    				$identity_status = $merchant1->check_status_jinjian;
    			}
    			$merchant_card  	= new Merchantinfocard();
    			$merchant2			= $merchant_card::findOne(['merchant_id' => $weixininfo->merchant_id]);
    			if(!empty($merchant2)){
    				$bank_no  		= !empty($merchant2->account_no)?$merchant2->account_no:'';
    				$bill_max		= !empty($merchant2->signle_time_amount)?$merchant2->signle_time_amount:'0.00';
    				$bill_max_day	= !empty($merchant2->signle_day_amount)?$merchant2->signle_day_amount:'0.00';
    			}
    			$wx_rates 	= Yii::$app->params['wx_rates'];
    			$zfb_rates 	= Yii::$app->params['zfb_rates'];
    			//$bill_rate	= '信用付:0.38% <br/> 微信:'.($wx_rates[0]*100).'% ; 支付宝:'.($zfb_rates[0]*100).'%';
    			$bill_rate = '0.38%<br/>开通即送5000元免费收款大礼包';
    		}
    		if(!empty($id_card)){
    			$id_card = substr_replace($id_card,'***********',3,11);
    		}
    		if(!empty($bank_no)){
    			$bank_no = substr_replace($bank_no,'**********',3,10);
    		}
    		if(!empty($phone)){
    			$phone 	= substr_replace($phone,'*****',3,5);
    		}
    		$res['data'] = [
    			'openid'  			=> (string)$openid,
    			'banner'	  		=> (string)$banner,
    			'merchant_code'	  	=> (string)$merchant_code,
    			'merchant_name'	  	=> (string)$merchant_name,
    			'name'	  			=> (string)$name,
    			'id_card'	  		=> (string)$id_card,
    			'phone'	  			=> (string)$phone,
    			'address_detail'	=> (string)$address_detail,
    			'bank_no'	  		=> (string)$bank_no,
    			'bill_cycles'	  	=> (string)$bill_cycles,
    			'bill_max'  		=> (string)$bill_max,
    			'bill_max_day'	  	=> (string)$bill_max_day,
    			'bill_rate'	  		=> (string)$bill_rate,
    			'identy_status'		=> (string)$identity_status,
    			'headimg'			=> (string)$headimg,
    			'merchant_no'		=> (string)$merchant_no,
    			'min_amount'		=> (string)Yii::$app->params['min_amount'],
    		];
    		$res['code'] = '10000';
    		$res['msg']  = '成功';
    		$response=Yii::$app->response;
    		$response->format=Response::FORMAT_JSON;
    		$response->data=$res;
    	}else{
    		$res['code'] = '10402';
    		$res['msg']  = '请取消关注后重新关注公众号';
    		$response=Yii::$app->response;
    		$response->format=Response::FORMAT_JSON;
    		$response->data=$res;
    	}
    }

}